package service

import (
	"github.com/gin-gonic/gin"
	"github.com/astaxie/beego/orm"
	"talentsystem/model"
	"github.com/seefan/to"
)
//所有职位信息
func GetPosition(c*gin.Context){
	var param model.GetPosition
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	limit:=(param.Page-1)*param.Pagesize
	o:=orm.NewOrm()
	var msg []model.Position
	//_,err:=o.QueryTable("position").Offset(limit).Limit(param.Pagesize).All(&msg)

	query:=o.QueryTable("position").Offset(limit).Limit(param.Pagesize)
	if param.Type!=""{
		query=query.Filter("type",param.Type)
	}
	if param.Welfare!=""{
		query=query.Filter("welfare__icontains",param.Welfare)
	}
	if param.Area!=""{
		query=query.Filter("area__icontains",param.Area)
	}
	if param.Id!=0{
		query=query.Filter("id",param.Id)
	}
	_,err:=query.OrderBy("-id").All(&msg)
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	num,er:=query.Count()
	if er!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"total":  num,
		"data":msg,
	})
	return
}
//一个职位的信息
func GetOnePosition(c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	o:=orm.NewOrm()
	var msg []model.Position
	err:=o.QueryTable("position").Filter("id",param.Id).One(&msg)
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"data":msg,
	})
	return
}
//修改职位信息
func UpdatePosition(c *gin.Context){
	var param model.Position
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	o:=orm.NewOrm()
	_,err:=o.QueryTable("position").Filter("id",param.Id).Update(orm.Params{
		"positioncontent":param.PositionContent,
		"position":param.Position,
		"company":param.Company,
		"area":param.Area,
		"interview":param.Interview,
		"worktime":param.WorkTime,
		"edueational":param.Edueational,
		"sex":param.Sex,
		"agerange":param.AgeRange,
		"commission":param.Commission,
		"estimatecommission":param.EstimateCommission,
		"createtime":param.CreateTime,
		"workdaytime":param.WorkDayTime,
		"welfare":param.Welfare,
		"content":param.Content,
		"industry":param.Industry,
		"type":param.Type,
		"wages":param.Wages,
	})
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":"修改成功",
	})
	return
}
//插入职位信息
func InsertPosition(c*gin.Context){
	var param model.InsertPosition
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	o:=orm.NewOrm()
	position:= new(model.Position)
	position.PositionContent =param.PositionContent
	position.Position=param.Position
	position.Company =param.Company
	position.Area =param.Area
	position.Interview=param.Interview
	position.WorkTime=param.WorkTime
	position.Edueational=param.Edueational
	position.Sex=param.Sex
	position.AgeRange=param.AgeRange
	position.Commission=param.Commission
	position.EstimateCommission=param.EstimateCommission
	position.CreateTime=param.CreateTime
	position.WorkDayTime=param.WorkDayTime
	position.Welfare=param.Welfare
	position.Content=param.Content
	position.Industry=param.Industry
	position.Type=param.Type
	position.Wages=param.Wages
	position.CompanyId=param.CompanyId
	_,err:=o.Insert(position)
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":"添加成功",
	})
	return
}
func AddCommission (c*gin.Context){
	var param model.TypeAddCommission
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	value:=to.Int64(param.Fphone)
	var msg model.AccountHeadhunters
	err:=orm.NewOrm().Raw("SELECT id,commission FROM account_headhunters WHERE phone=? OR id=?",param.Fphone,value).QueryRow(&msg)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系統错误请刷新",
		})
		return
	}
	if msg.Id==0{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"没有找到该用户",
		})
		return
	}else {
		var position model.Position
		err:=orm.NewOrm().QueryTable("position").Filter("id",param.PositionId).One(&position,"commission")
		if err!=nil {
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系統错误请刷新",
			})
			return
		}
		num,err:=orm.NewOrm().QueryTable("account_headhunters").Filter("id",msg.Id).Update(orm.Params{
			"Commission":msg.Commission+position.Commission,
		})
		if err!=nil {
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系統错误请刷新",
			})
			return
		}
		if num ==0{
			c.JSON(403,gin.H{
				"status":403,
				"msg":"累加佣金失败",
			})
			return
		}
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
	})
	return
}
func DelPosition (c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	num,err:=orm.NewOrm().QueryTable("position").Filter("id",param.Id).Delete()
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系統错误请刷新",
		})
		return
	}
	if num ==0{
		c.JSON(403,gin.H{
			"status":403,
			"msg":0,
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
	})
	return
}